﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using Common;

public partial class tools_sysrightsmaglist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        //if (Profile.AccountInfo.UserName != "admin")
        //{
        //    Response.Redirect("../unauthorized.htm?errcode=5");
        //    Response.End();
        //}

        switch (RequestBase.GetFormString("otype"))
        {
            case "loaddata":
                LoadingData();
                break;
            case "getfunctionlist":
                LoadFunctionDataList();
                break;
            case "delete":
                Delete();
                break;
        }

        DropdownListDataBind();
    }

    #region 下拉菜单数据邦定
    void DropdownListDataBind()
    {
        DataTable dt = BLL.CommonBLL.Select(
            "System_FunctionCategory",
            "FunctionCategoryId, CategoryName",
            " ORDER BY LineNum ASC");

        dropCategory.Items.Add(new ListItem("-- 全部 --", ""));
        foreach (DataRow dr in dt.Select(""))
        {
            dropCategory.Items.Add(new ListItem(dr["CategoryName"].ToString().Trim(), dr["FunctionCategoryId"].ToString().Trim()));
        }
        dt.Dispose();

        dropCategory.Attributes.Add("onchange", "LoadFunctionDataList();");
    }
    /// <summary>
    /// 获取子类明细
    /// </summary>
    void LoadFunctionDataList()
    {
        string categoryid = RequestBase.GetFormString("categoryid");
        StringBuilder selOptions = new StringBuilder();
        string items = "<option value=\"{0}\">{1}</option>";

        DataTable dat = BLL.CommonBLL.Select("vw_SystemFunctions", "[FunctionId], [FunctionName]", " AND FunctionCategoryId = '" + RequestBase.GetFormString("categoryid") + "' ORDER BY LineNum ASC");
        selOptions.Append(string.Format(items, "", "-- 全部 --"));
        foreach (DataRow dr in dat.Select(""))
        {
            selOptions.Append(string.Format(items, dr["FunctionId"].ToString(), dr["FunctionName"].ToString()));
        }

        dat.Clear();
        dat.Dispose();
        BLL.AjaxHelper.ResponseWrite(selOptions.ToString());
    }
    #endregion

    #region 信息绑定
    void LoadingData()
    {
        string ctrl = "~/admin/system/_controls/ctlsysfunction.ascx";

        string SQL = "";
        if (!string.IsNullOrEmpty(RequestBase.GetFormString("categoryid")))
            SQL += " AND FunctionCategoryId = '" + RequestBase.GetFormString("categoryid") + "'";

        if (!string.IsNullOrEmpty(RequestBase.GetFormString("functionid")))
            SQL += " AND FunctionId = '" + RequestBase.GetFormString("functionid") + "'";

        Control uc_rep = LoadControl(ctrl);

        Repeater rptSysRightList = (Repeater)uc_rep.FindControl("rptSysRightList");

        DBUtility.Pagination pagination = new DBUtility.Pagination();
        try
        {
            if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
            else
                pagination.Sequence = 1;

            pagination.ItemUnit = "条权限记录";
            pagination.SortSentence = " ORDER BY FunctionName, LineNum ASC";
            pagination.Primarykey = "PrivilegeId";
            pagination.SearchField = "*";
            pagination.TableName = "vw_SystemPrivileges";
            pagination.PageSize = 50;
            pagination.UrlStr = "";
            pagination.ImagesPath = "../_imgs/grid/";
            pagination.SearchSentence = SQL;

            rptSysRightList.DataSource = pagination.GetDataTableFormServicesDB();
            rptSysRightList.DataBind();

            pagination.PageBarFerrari();
            uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);

            uc_rep.GetType().GetProperty("TotalInfo").SetValue(uc_rep, "共计 " + pagination.RecordCount.ToString() + pagination.ItemUnit, null);

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            rptSysRightList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
    #endregion

    #region 删除
    void Delete()
    {
        string res = "";
        bool bl = true;
        if (!bl)
        {
            res = "只有系统管理员才有执行此操作的权限!";
        }
        else
        {
            if (!Common.Utils.IsGuid(RequestBase.GetFormString("privilegeid")))
            {
                res = "参数错误!";
            }
            else
            {
                BLL.CommonBLL.Delete("System_FunctionPrivilegeBase", " PrivilegeId = '" + RequestBase.GetFormString("privilegeid") + "'", true);
                res = "succeeded";
            }
        }
        BLL.AjaxHelper.ResponseWrite(res);
    }
    #endregion
}
